/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.resources;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.PerforceSynchronizer;
import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.syncinfo.FolderSyncInfo;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;

/**
  * @version $Revision: 75 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class LocalFolder extends LocalResource
{
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Call this method to create a LocalFolder instance.
	  * 
	  * @param server the perforce server.
	  * @param location the local path.
	  * @return the LocalFolder instance.
	  */
	public static final LocalFolder create(IPerforceServer server, IPath location)
	{
		return new LocalFolder(server, location);
	} 
	
	// -------------------------------------------------------------------------------- Constructors
	
	/** Constructor for LocalFolder.
	  * 
	  * @see LocalResource#LocalResource(IPerforceServer, IPath)
	  */
	private LocalFolder(IPerforceServer server, IPath location)
	{
		super(server, location);
	}

	// ------------------------------------------------------------------------------ Public Methods

	/** Checks whether the folder exists on the server.
	  * 
	  * @return true if the folder exists.
	  */
	public boolean hasRemote()
	{
		try
		{
			FolderSyncInfo syncInfo =
				PerforceCorePlugin.getSynchronizer().getFolderSync((IContainer) toResource());
				
			return  syncInfo != null;
		}
		catch (PerforceException e)
		{
			PerforceCorePlugin.log(e.getStatus());
			return false;
		}
	}

	/** Returns the contained files.
	  * 
	  * @return array of LocalFile.
	  * 
	  * @throws PerforceException in case of an error.
	  */
	public LocalFile[] getFiles()
		throws PerforceException
	{
		try
		{
			IContainer container = (IContainer) toResource();
			IResource[] members = container.members();
			
			List result = new ArrayList();
			for (int i = 0, length = members.length; i < length; i++)
			{
				if (members[i].getType() == IResource.FILE)
				{
					result.add(LocalFile.create(getServer(), members[i].getLocation()));
				}
			}
			
			return (LocalFile[]) result.toArray(new LocalFile[0]);
		}
		catch (CoreException e)
		{
			throw new PerforceException(e.getStatus());
		}
	}
	
	/** Returns the contained folders.
	  * 
	  * @return array of LocalFolder.
	  * 
	  * @throws PerforceException in case of an error.
	  */
	public LocalFolder[] getFolders()
		throws PerforceException
	{
		try
		{
			IContainer container = (IContainer) toResource();
			IResource[] members = container.members();
			
			List result = new ArrayList();
			for (int i = 0, length = members.length; i < length; i++)
			{
				if (members[i].getType() != IResource.FILE)
				{
					result.add(LocalFolder.create(getServer(), members[i].getLocation()));
				}
			}
			
			return (LocalFolder[]) result.toArray(new LocalFolder[0]);
		}
		catch (CoreException e)
		{
			throw new PerforceException(e.getStatus());
		}
	}	

	/** Folder cannot be checked out.
	  * 	
	  * @return false
	  */
	public boolean isCheckedOut()
	{
		return false;
	}	

	/** Folder cannot be dirty.
	  * 	
	  * @return false
	  */
	public boolean isDirty()
	{
		return false;
	}	

	/** Folder cannot be out of date.
	  * 	
	  * @return false
	  */
	public boolean isOutOfDate()
	{
		return false;
	}
	
	/** Returns the FolderSyncInfo of the container.
	  * 
	  * @return the FolderSyncInfo.
	  */
	public FolderSyncInfo getFolderSync()
	{
		try
		{
			return PerforceSynchronizer.getInstance().getFolderSync((IContainer) toResource());
		}
		catch (PerforceException e)
		{
			PerforceCorePlugin.log(e.getStatus());
		}
		
		return null;
	}

	/** Folders do not have ResourceSyncInfo.
	  * 	
	  * @return null
	  */
	public ResourceSyncInfo getSyncInfo()
	{
		return null;
	}
	
	/** @see net.sourceforge.perforce.core.resources.LocalResource#toResource() */
	public IResource toResource() 
	{
		IWorkspaceRoot root = PerforceCorePlugin.getWorkspace().getRoot();
		
		// WORKAROUND: Check with Eclipse Team for other possibility
		return root.getFileForLocation(getLocation().append("dummy")).getParent();
	}
}
